using System;
using Habanero.Base;
using Habanero.BO;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// If the solution has been generated then it can no longer be changed.
    /// </summary>
    public class SolutionNameCanBeEdited : IBOPropAuthorisation
    {
        private readonly IBOProp _generatedProp;

        public SolutionNameCanBeEdited(BOProp boProp, IBOProp generatedProp)
        {
            if (boProp == null) throw new ArgumentNullException("boProp");
            _generatedProp = generatedProp;
        }

        #region IBOPropAuthorisation Members

        public void AddAuthorisedRole(string authorisedRole, BOPropActions actionToPerform)
        {
        }

        public bool IsAuthorised(IBOProp boProp, BOPropActions actionToPerform)
        {
            if (actionToPerform == BOPropActions.CanRead)
            {
                return true;
            }

            object generated = _generatedProp.Value;
            bool isGenerated;
            if (generated == null) return true;
            if (bool.TryParse(Convert.ToString(generated), out isGenerated))
            {
                return !isGenerated;
            }
            return false;
        }

        #endregion
    }
}